﻿using System;
using System.Collections.Generic;
using System.Linq;
using UMC.Web;
using UMC.Data.Entities;
using UMC.Web.UI;
using UMC.ITME.Entities;
using UMC.Data;
namespace UMC.ITME.Activities
{
    /// <summary>
    /// 邮箱账户
    /// </summary>
    [UMC.Web.Apiumc("ITME", "Online", Auth = WebAuthType.Guest)]
    public class SiteOnlineActivity : WebActivity
    {
        class Online
        {
            public int Day;
            public int OnLine;
            public String Root;
        }

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var h24 = DateTime.Now.AddDays(-1);
            var onTime = DateTime.Now.AddMinutes(-30);
            int index = 0;
            var model = this.AsyncDialog("Model", r =>
            {

                var h24M = 0;
                var Online = 0;
                while (index > -1)
                {
                    var sessions = HotCache.Cache<Session>().Find(new Session(), index, out index);
                    foreach (var session in sessions)
                    {

                        if (session.UpdateTime > onTime)
                        {
                            Online++;
                            h24M++;
                        }
                        else if (session.UpdateTime > h24)
                        {
                            h24M++;
                        }

                    }
                }
                var th24 = Utility.TimeSpan(h24);
                var tonTime = Utility.TimeSpan(onTime);
                Dictionary<String, Online> onlines = new Dictionary<String, Online>();
                index = 0;
                while (index > -1)
                {
                    var sessions = HotCache.Cache<Cookie>().Find(new Cookie(), index, out index);
                    foreach (var session in sessions)
                    {
                        int l = session.Domain.IndexOf('_');
                        if (l > 0)
                        {
                            // if (session.Domain.EndsWith("_Main"))
                            // {
                            session.Domain = session.Domain.Substring(0, l);//session.Domain.Length - 5);
                            // }
                            // else
                            // {
                            //     continue;
                            // }
                        }
                        if (onlines.TryGetValue(session.Domain, out var online) == false)
                        {
                            online = new Online() { Root = session.Domain };

                            if (session.LoginTime > tonTime)
                            {
                                online.Day++;
                                online.OnLine++;
                                onlines[session.Domain] = online;
                            }
                            else if (session.LoginTime > th24)
                            {

                                online.Day++;
                                onlines[session.Domain] = online;
                            }
                        }
                        else
                        {
                            if (session.LoginTime > tonTime)
                            {
                                online.Day++;
                                online.OnLine++;
                            }
                            else if (session.LoginTime > th24)
                            {

                                online.Day++;
                            }
                        }
                    }
                }

                var ui = UISection.Create(new UITitle("人员在线"));
                ui.Add(new UITextNameValue("ITME", $"{h24M}", $"{Online}").Click(new UIClick(request, r, "User")));

                var ui2 = ui.NewSection();
                foreach (var line in onlines.Values.OrderByDescending(r => r.Day))
                {
                    ui2.Add(new UITextNameValue(line.Root, $"{line.Day}", $"{line.OnLine}"));
                }
                ui.SendTo(this.Context);

            });

            var ui2 = UISection.Create(new UITitle("在线人员"));
            while (index > -1)
            {
                var sessions = HotCache.Cache<Session>().Find(new Session(), index, out index);
                foreach (var session in sessions)
                {

                    if (session.UpdateTime > onTime)
                    {

                        var Value = UMC.Data.JSON.Deserialize<Data.AccessToken>(session.Content.Span);
                        if (Value != null && String.Equals(Value.Username, "?") == false)
                        {
                            ui2.AddCell(Value.Alias, Utility.GetDate(session.UpdateTime), new UIClick("Account", "Staff", Value.Username));
                        }
                    }

                }
            }

            ui2.SendTo(this.Context);
        }
    }
}